package com.allwees.core.common.mvc.converter;

import com.allwees.core.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.core.convert.converter.Converter;

import java.util.Date;

public class StringLongToDateConverter implements Converter<String, Date> {
	@Override
	public Date convert(String source) {
		if(StringUtils.isBlank(source)) {
			return null;
		}
		if(NumberUtils.isCreatable(source)) {
			return new Date(Long.valueOf(source));
		}

		return DateUtil.parseDate(source, DateUtil.DEFAULT_DATETIME_FORMAT, DateUtil.DEFAULT_DATE_FORMAT);
	}
}
